import request from '@/utils/request';
import qs from 'qs';

export interface LoginParamsType {
  userName: string;
  password: string;
  mobile: string;
  captcha: string;
}

export interface LoginParamsProps {
  userName: string;
  password: string;
  mobile: string;
  captcha: string;
}

export async function fakeAccountLogin(params: LoginParamsType) {
  return request('/api/login/account', {
    method: 'POST',
    data: params,
  });
}

export async function getFakeCaptcha(mobile: string) {
  return request(`/api/login/captcha?mobile=${mobile}`);
}

export async function AccountLogin(params: LoginParamsProps) {
  params["nickname"] = params.userName;
  return request('/api/user/login', {
    method: 'PUT',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
    },
    data: qs.stringify(params, { arrayFormat: 'indices', allowDots: true }),
  }).then((response) => {
    let obj = new Object;
    obj["type"] = params.type;
    if(response){
      if (response.code === 200) {
        obj["status"] = "ok";
        obj["message"] = "success";
        obj["data"] = response.data;
        obj["currentAuthority"]= response.data.permissionSet;
      }else{
        obj["status"] = "error";
        obj["message"] = response.message;
        obj["data"] = "";
      }
    }else{
      obj["status"] = "error";
      obj["message"] = "网络错误，请求失败";
      obj["data"] = "";
    }
    return obj;
  });
}